var express =require('express')
var router = express.Router();
const{uploadFiles,moveFiles, removeFiles}=require("../../utils/uploadFiles")
const { getproduct,addproduct,renoveone,getupdataproductID,updataproduct ,getgoods,show} = require('../../service/store/storeService');
//库存渲染，获取所有库存
   router.get("/getproduct",async function(req, res, next){
    var data=await getproduct(req.query)
      res.send(data)
   })
    

 //库存新增addproduc
 router.post("/addproduc",async function(req,res,next){

   var datas = req.body
   var data=await addproduct(datas)
      res.send(data)
 })
 //库存删除
 router.post("/renoveone",async function(req, res, next){ 
   var data=await renoveone(req.body)
     res.send(data)
  });
  //获取需要修改库存的ID
  router.post("/getupdataproductID",async function(req, res, next){
    const {_id}=req.body.id;
    // console.log('第一层',_id);
    var datas=await getupdataproductID(_id)
      res.send(datas)
   });
   router.post("/updataproduct",async function(req, res, next){
    
    var datas=await updataproduct(req.body)
      res.send(datas)
   });
  // 图片的保存  组件
  var updateImg = uploadFiles({
  path: './public/updateImg',
  key: "file",
});
var addImg = uploadFiles({
  path: './public/addImg',
  key: "file",
});
// 获取图片选择的图片的名称
router.post('/uploadImgs', updateImg, async function (req, res, next) {
  var data = (req.files).filter((item) => {
      return item.size < (1024 * 1024 * 2)
  });
  var imgName = data.map((item, index) => { return "updateImg/"+item.filename });
  res.send(imgName);

})
// 获取图片选择的图片的名称
router.post('/addImgs', addImg, async function (req, res, next) {
  var data = (req.files).filter((item) => {
      return item.size < (1024 * 1024 * 2)
  });
  var imgName = data.map((item, index) => { return item.filename });
  res.send(imgName);
})
router.post("/quireInventory", async function (req, res, next) {
  var data = req.body;
  res.send(await quireInventory(data));
})


//商品上架列表的渲染getgoods
router.get("/getgoods",async function(req, res, next){
  // console.info("第1层","req.query")
  var data=await getgoods()
  // console.info("数据",data)
    res.send(data)
 })
 router.post("/showshop",async function(req, res, next){
  console.info("第1层",req.body)
  var data=await show(req.body)
  // console.info("数据",data)
    res.send(data)
 })
module.exports=router